const path = require('path');

var webpack = require("webpack");
var ExtractTextPlugin = require('extract-text-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');

//环境变量配置  dev | online
var WEBPACK_ENV = process.env.WEBPACK_ENV || 'dev';
console.log(WEBPACK_ENV);
var getHtmlConfig = function (name) {

    return {
        template: './src/view/' + name + '.html',
        filename: 'view/' + name + '.html',
        inject: true,
        hash: true,
        chunks: ['common', name]
    };
};

var config = {
    mode: 'development',
    entry: {

        'index': ['./src/page/index/index.js'],
        'login': ['./src/page/login/index.js'],
        'common': ['./src/page/common/index.js'],

    },
    output: {
        filename: 'js/[name].js',
        path: path.resolve(__dirname, 'dist'),
        publicPath: "/dist"
    },
    externals: {
        jquery: "window.jQuery"
    },
    plugins: [
        //把css文件单独打包到文件里
        new ExtractTextPlugin('css/[name].css'),
        //html模板的处理
        new HtmlWebpackPlugin(getHtmlConfig("index")),
        new HtmlWebpackPlugin(getHtmlConfig("login")),
    ],
    // 独立通用模块
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: "common",
                    chunks: "initial",
                    minChunks: 2

                }
            }
        }
    },
    module: {
        rules: [
            {
                test: /\.css$/,
                loader: ExtractTextPlugin.extract( "css-loader","style-loader")
            },
            {
                test: /\.(git|png|jpg|woff|svg|eot|ttf)\??.*$/,
                loader: "url-loader?limit=100&name=../resource/[name].[ext]"
            },
            {
                test: /\.html$/,
                use: [{
                    loader: "html-loader",
                    options: {
                        interpolate: true
                    }
                }]
            }
        ]
    },
    resolve:{
        alias:{
            node_modules:path.resolve(__dirname, "./node_modules"),
            util:path.resolve(__dirname, "./src/util"),
            page:path.resolve(__dirname, "./src/page"),
            image:path.resolve(__dirname, "./src/image"),
            service:path.resolve(__dirname, "./src/service")
        }
    }
};

if('dev'===WEBPACK_ENV){
    config.entry.common.push("'webpack-dev-server/client?http://localhost:8088/'");
}
module.exports = config;